#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> 
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "info.h"


#define ERR_LOG(msg) do{ \
    printf("%s-%s-%d\n",__FILE__,__func__,__LINE__); \
    perror(msg);  \
    exit(1); \
    }while(0)

#define N 128

typedef struct{
    int sockfd;
    int acceptfd;
    struct sockaddr_in client_addr;
}MSG;


char buf[N];

//服务器初始化
void server_init(const char *arg1, const char *arg2);

//服务器获取客户端数据
int get_data_server(int sockfd,int acceptfd,struct sockaddr_in client_addr);

//线程处理函数
void *process_client(void *arg);

void process_client_request_admin(INFO_MSG info_msg);

void do_query_admin(void);
void do_modify_admin(void);
void do_add_admin(void);
void do_delete_admin(void);
void do_history_admin(void);

void process_client_request_user(INFO_MSG info_msg);
void do_query_user(void);
void do_modify_user(void);



int main(int argc, char const *argv[])
{
    
    if(argc != 3)
    {
        fprintf(stderr,"Usage: %s <ip> <port>\n",argv[0]);
        exit(1);
    }

    server_init(argv[1],argv[2]);
    return 0;
}


void server_init(const char *arg1, const char *arg2)
{
    int sockfd;
    struct sockaddr_in server_addr,client_addr;
    int acceptfd;
    pthread_t pid;
    MSG msg;

    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0) 
    {
        ERR_LOG("socket failed");
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(arg1);
    server_addr.sin_port = htons(atoi(arg2));
    socklen_t addrlen = sizeof(struct sockaddr_in);
    if(bind(sockfd,(struct sockaddr *)&server_addr,addrlen) < 0)
    {
        ERR_LOG("bind failed");
    }

    if(listen(sockfd,5) < 0)
    {
        ERR_LOG("listen failed");
    }

    while(1)
    {
        if((acceptfd = accept(sockfd,(struct sockaddr *)&client_addr,&addrlen)) < 0)
        {
            ERR_LOG("accept failed");
        }
        printf("%s:%d连接了\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));

        msg.sockfd = sockfd;
        msg.acceptfd = acceptfd;
        msg.client_addr = client_addr;
        
        if (pthread_create(&pid, NULL,process_client,&msg) != 0)
        {
            ERR_LOG("fail to create pthread");
        }

        pthread_detach(pid);
    }

    return ;
}

// 线程处理函数
void *process_client(void *arg)
{
    MSG msg = *(MSG *)arg;
    int ret;
    while(1)
    {
        //测试
        //send(msg.acceptfd, &msg, sizeof(msg), 0);

        if ((ret = get_data_server(msg.sockfd,msg.acceptfd,msg.client_addr)) == 1)
        {
            break;
        }

        //
    }

    return NULL;
}

//客户端与服务器之间的数据交互
int get_data_server(int sockfd,int acceptfd,struct sockaddr_in client_addr)
{
    ssize_t bytes;
    INFO_MSG info_msg;     //双方通信结构体信息
    if((bytes = recv(acceptfd,&info_msg,sizeof(info_msg),0)) == -1)
    {
        ERR_LOG("fail to recv");
    }       
    else if(bytes == 0)
    {
        printf("%s:%d退出了连接\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
        return 1;
    }
    else      //接收到客户端数据
    {
        if(info_msg.usertype == 1)   //管理员
        {
            if (strncmp(info_msg.recvmsg,"quit",4) == 0)
            {
                printf("%s:%d退出了连接\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
                close(acceptfd);
                return 1;
            }
            else
            {
                process_client_request_admin(info_msg);
                // printf("client :%s\n",info_msg.recvmsg);
                // strcat(info_msg.recvmsg,"*_*");
                // if (send(acceptfd,&info_msg,sizeof(info_msg),0) == -1)
                // {
                //     ERR_LOG("fail to send");
                // }
            }
            
        }
        else if (info_msg.usertype == 2)   //普通用户
        {
            if (strncmp(info_msg.recvmsg,"quit",4) == 0)
            {
                printf("%s:%d退出了连接\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
                close(acceptfd);
                return 1;
            }
            else
            {
                process_client_request_user(info_msg);
                // printf("client:%s\n",info_msg.recvmsg);
                // strcat(info_msg.recvmsg,"*_*");
                // if (send(acceptfd,&info_msg,sizeof(info_msg),0) == -1)
                // {
                //     ERR_LOG("fail to send");
                // }
            }
            
        }
        else
        {
            printf("不存在这类用户\n");
            return 1;
        }
        

    }

    return 0;
}

//处理管理员请求
void process_client_request_admin(INFO_MSG info_msg)
{
    switch(info_msg.msgtype)
    {
        case QUERY:
            do_query_admin();
            break;
        case MODIFY:
            do_modify_admin();
            break;
        case ADD:
            do_add_admin();
            break;
        case DELETE:
            do_delete_admin();
            break;
        case HISTORY:
            do_history_admin();
            break;
        default:
            printf("无效数据\n");
    }
    return ;
}

void do_query_admin(void)
{
    printf("管理员查询\n");
}

void do_modify_admin(void)
{
    printf("管理员修改\n");
}

void do_add_admin(void)
{
    printf("管理员添加用户\n");
}

void do_delete_admin(void)
{
    printf("管理员删除用户\n");
}

void do_history_admin(void)
{
    printf("管理员查询历史记录\n");
}


//处理普通用户请求
void process_client_request_user(INFO_MSG info_msg)
{
    switch(info_msg.msgtype)
    {
        case QUERY:
            do_query_user();
            break;
        case MODIFY:
            do_modify_user();
            break;
        default:
            printf("无效数据\n");
    }
    return ;
}

void do_query_user(void)
{
    printf("用户查询\n");
}

void do_modify_user(void)
{
    printf("用户修改\n");
}











